package com.gaoqi.blueversion.provider.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * MyBatis基础配置
 *
 * @author Administrator
 */
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {

    // 精确到 cluster 目录，以便跟其他数据源隔离
    //mapper接口所在的包
    static final String PACKAGE = "com.gaoqi.blueversion.dao";
    //映射文件目录
    private static final String MAPPER_LOCATION = "classpath:com.gaoqi.blueversion.dao/*.xml";
    //哪个包下面的类自动配置别名
    //private static final String ALIAS = "com.gaoqi.blueversion.model";

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username")
    private String user;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driverClassName}")
    private String driverClass;

    public DataSourceConfig() {
    }
    /**
     * 创建数据源对象
     *
     * @return
     * @throws
     */
    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSourceConfig() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(user);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClass);
        return datasource;
    }


    @Bean(name = "testTransactionManager")
    public DataSourceTransactionManager clusterTransactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(dataSourceConfig());
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource clusterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clusterDataSource);

        // 加载全局的配置文件
        /*sessionFactory.setConfigLocation(
                new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));*/

        // 配置类型别名
        //sessionFactory.setTypeAliasesPackage(ALIAS);

        //设置mapper映射文件所在的地址
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(DataSourceConfig.MAPPER_LOCATION));

        return sessionFactory.getObject();
    }
}

